package com.road7.mina.withprotobuf;

import java.net.InetSocketAddress;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.road.yishi.pb.active.StudentMsgProto;
import com.road7.mina.customCodec.ProtobufStrictCodecFactory;

/**
 * @Description :
 * @FileName: Client.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年11月20日 下午5:50:49
 * @Version:V1.00
 */
public class Client {

	public static void main(String[] args) {
		IoConnector connector = new NioSocketConnector(Runtime.getRuntime().availableProcessors() + 1);

		connector.getFilterChain().addLast("logger", new LoggingFilter());
		connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ProtobufStrictCodecFactory())); // 设置编码过滤器

		connector.setConnectTimeoutMillis(30000);
		connector.setHandler(new ClientHandler("客户端hanlder"));
		ConnectFuture cf = connector.connect(new InetSocketAddress("127.0.0.1", Server.PORT));// 建立连接
		cf.awaitUninterruptibly();// 等待连接创建完成

		IoSession session = cf.getSession();

		
		
		StudentMsgProto.StudentRspMsg.Builder builder = StudentMsgProto.StudentRspMsg.newBuilder();
		builder.setId(9);
		builder.setName("服务器");
		builder.setEmail("123@abc.com");
		builder.addFriends("X");
		builder.addFriends("Y");
		StudentMsgProto.StudentRspMsg student2 = builder.build();
		session.write(student2);
		
		session.getCloseFuture().awaitUninterruptibly();// 等待连接断开
		connector.dispose();
	}

}
